﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.missions {
    import flash.events.*;
    import reprise.controls.*;
    import org.robotlegs.core.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.world.*;

    public class MissionBrowserMediator extends Mediator implements IMediator {

        private const MAX_SHOWN_MISSIONS:int = 5;

		[Inject]
        public var missionModel:MissionModel;
        private var arrowUp:SimpleButton;
        private var arrowDown:SimpleButton;
        private var scrollOffset:int;// = 0

        override public function onRegister():void{
            addContextListener(EventConstants.ADD_MISSION, this.addMission);
            addContextListener(EventConstants.REMOVE_MISSION, this.removeMission);
            addContextListener(EventConstants.ENABLE_MISSION_BUTTON, this.onEnabled);
            addContextListener(EventConstants.HIDE_MISSIONS, this.onHide);
            addContextListener(EventConstants.SHOW_MISSIONS, this.onShow);
            this.initArrowButtons();
            this.initMissions();
        }
        private function initArrowButtons():void{
            this.arrowUp = (RepriseUtils.addComponent(this.view, "scrollArrowButton upArrowButton", SimpleButton) as SimpleButton);
            this.arrowUp.hide();
            eventMap.mapListener(this.arrowUp, MouseEvent.CLICK, this.onArrowUp);
            this.arrowDown = (RepriseUtils.addComponent(this.view, "scrollArrowButton downArrowButton", SimpleButton) as SimpleButton);
            this.arrowDown.hide();
            eventMap.mapListener(this.arrowDown, MouseEvent.CLICK, this.onArrowDown);
        }
        private function onArrowDown(_arg1:MouseEvent):void{
            this.scrollOffset++;
            this.view.removeMissionOnPos(0);
            var _local2:int = (this.MAX_SHOWN_MISSIONS + this.scrollOffset);
            l(("index: " + _local2));
            var _local3:MissionVO = this.missionModel.allMissions[(_local2 - 1)];
            this.view.addMission(_local3, (this.MAX_SHOWN_MISSIONS - 1));
            this.updateArrowVisibillity();
        }
        private function onArrowUp(_arg1:MouseEvent):void{
            this.scrollOffset--;
            this.view.removeMissionOnPos((this.MAX_SHOWN_MISSIONS - 1));
            var _local2:int = this.scrollOffset;
            l(("index: " + _local2));
            var _local3:MissionVO = this.missionModel.allMissions[_local2];
            this.view.addMission(_local3);
            this.updateArrowVisibillity();
        }
        private function onHide(_arg1:Event):void{
            this.setEnabled(false);
        }
        private function onShow(_arg1:Event):void{
            this.setEnabled(true);
        }
        private function onEnabled(_arg1:InfoEvent):void{
            var _local2:Boolean = Boolean(_arg1.info);
            this.setEnabled(_local2);
        }
        private function setEnabled(_arg1:Boolean):void{
            if (_arg1){
                this.updateArrowVisibillity();
            } else {
                this.arrowDown.hide();
                this.arrowUp.hide();
            };
        }
        private function addMission(_arg1:InfoEvent):void{
            this.scrollOffset = 0;
            this.view.addMission((_arg1.info as MissionVO));
            this.view.removeMissionOnPos(this.MAX_SHOWN_MISSIONS);
        }
        private function removeMission(_arg1:InfoEvent):void{
            this.scrollOffset = 0;
            this.view.removeMission((_arg1.info as MissionVO));
        }
        private function initMissions():void{
            var _local1:int = (this.MAX_SHOWN_MISSIONS - 1);
            while (_local1 >= 0) {
                this.view.addMission(this.missionModel.allMissions[_local1]);
                _local1--;
            };
            this.updateArrowVisibillity();
        }
        private function updateArrowVisibillity():void{
            if (this.missionModel.allMissions.length > this.MAX_SHOWN_MISSIONS){
                this.arrowDown.show();
                this.arrowUp.show();
                this.updateArrowStates();
            } else {
                this.arrowDown.hide();
                this.arrowUp.hide();
            };
        }
        private function updateArrowStates():void{
            if (this.atTop()){
                this.arrowUp.addCSSClass("disable");
                this.arrowUp.enabled = false;
            } else {
                this.arrowUp.removeCSSClass("disable");
                this.arrowUp.enabled = true;
            };
            if (this.atBottom()){
                this.arrowDown.addCSSClass("disable");
                this.arrowDown.enabled = false;
            } else {
                this.arrowDown.removeCSSClass("disable");
                this.arrowDown.enabled = true;
            };
        }
        private function atTop():Boolean{
            return ((this.scrollOffset == 0));
        }
        private function atBottom():Boolean{
            return (((this.scrollOffset + this.MAX_SHOWN_MISSIONS) >= this.missionModel.allMissions.length));
        }
        public function get view():MissionBrowserView{
            return ((viewComponent as MissionBrowserView));
        }

    }
}//package net.wooga.pets.views.missions 
